package com.liming.cart.controller;

import com.liming.cart.service.CartItemMircoService;
import com.liming.cart.service.CartMircoService;
import com.liming.common.pojo.dto.cart.AddItemToCartDTO;
import com.liming.common.pojo.vo.cart.openfeign.FeignProductInCartVO;
import com.liming.common.result.LMResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liming
 * @date 2025/1/17
 */
@Slf4j
@RestController
@AllArgsConstructor
@Tag(name = "购物车-Service")
@RequestMapping("/service/cart")
public class CartMircoController {

    private final CartMircoService cartMircoService;
    private final CartItemMircoService cartItemMircoService;

    @Operation(summary = "查询用户购物车商品信息")
    @GetMapping
    public LMResult<List<FeignProductInCartVO>> getProductInCart() {
        return LMResult.success(cartMircoService.getProductInCart());
    }

    @Operation(summary = "添加商品进入购物车")
    @PostMapping("addItem")
    public LMResult<Boolean> addItemToCart(@RequestBody AddItemToCartDTO dto) {
        return LMResult.success(cartMircoService.addItemToCart(dto));
    }

    @Operation(summary = "减少购物车中商品数量")
    @PutMapping("reduceItem/{id}")
    public LMResult<Boolean> reduceItemById(@PathVariable Long id) {
        return LMResult.success(cartItemMircoService.reduceItemById(id));
    }

    @PutMapping("addItem/{id}")
    public LMResult<Boolean> addItemById(@PathVariable Long id) {
        return LMResult.success(cartItemMircoService.addItemById(id));
    }

    @DeleteMapping("removeItems")
    public LMResult<Boolean> removeItemsByIds(@RequestBody List<Long> ids) {
        return LMResult.success(cartItemMircoService.removeItemsByIds(ids));
    }

    @DeleteMapping("removeItemsByProductIds")
    public LMResult<Boolean> removeItemsByProductIds(@RequestBody List<Long> productIds) {
        Long cartId = cartMircoService.getUserCart().getId();
        return LMResult.success(cartItemMircoService.removeItemsByProductIds(cartId, productIds));
    }
}
